package com.ybg.bookmember.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ybg.bookcommons.dto.ReservationsDTO;
import com.ybg.bookmember.mapper.BookinfosMapper;
import com.ybg.bookmember.mapper.ReservationsMapper;
import com.ybg.bookmember.mapper.UserinfosMapper;
import com.ybg.bookmember.module.entity.Bookinfos;
import com.ybg.bookmember.module.entity.Reservations;
import com.ybg.bookmember.module.entity.Userinfos;
import com.ybg.bookmember.services.ReservationsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReservationsServiceImpl extends ServiceImpl<ReservationsMapper, Reservations>
    implements ReservationsService {
    @Resource
    private BookinfosMapper bookinfosMapper;
    @Resource
    private UserinfosMapper userinfosMapper;
    @Override
    public List<ReservationsDTO> reserv(String isbn, long userid) {
        //1.根据isbn查询图书信息
        LambdaQueryWrapper<Bookinfos> bookQuery = new LambdaQueryWrapper<Bookinfos>();
        bookQuery.eq(Bookinfos::getIsbn, isbn);


        Bookinfos book = bookinfosMapper.selectOne(bookQuery);
        //2.根据图书信息封装对象
        Reservations re =Reservations.builder().bookid(book.getBookid())
                .userid(userid).resdate(new Date())
                .deleted(0L)
                .build();
        //3.将借阅信息保存到数据库
        save(re);
        //4.查询该书所有借阅的信息
        LambdaQueryWrapper<Reservations> resQuery = new LambdaQueryWrapper<Reservations>()
                .eq(Reservations::getBookid, book.getBookid());
        List<Reservations> list = list(resQuery);
        //根据一批userid查询一堆用户名
        //4.1 获取所有用户的id
        List<Long> userids = list.stream().map(u -> u.getUserid()).collect(Collectors.toList());
        //4.2 根据所有用户的id查询用户信息
        Map<Long, String> realnames = userinfosMapper.selectByIds(userids).stream()
                .collect(Collectors.toMap(Userinfos::getUserid, Userinfos::getRealname));
        //4.3 将所有的Reservations对象更换为ReservationsDTO
        List<ReservationsDTO> rdtos = list.stream().map(r ->
                ReservationsDTO.builder().resdate(r.getResdate())
                .realname(realnames.get(r.getUserid()))
                .bookname(book.getBookname())
                .build()).collect(Collectors.toList());

        return rdtos;
    }

    @Override
    public boolean destoryReserv(String isbn, long userid) {
        try {
            LambdaQueryWrapper<Bookinfos> bookQuery = new LambdaQueryWrapper<Bookinfos>();
            bookQuery.eq(Bookinfos::getIsbn, isbn);
            Bookinfos book = bookinfosMapper.selectOne(bookQuery);
            LambdaQueryWrapper<Reservations> resQuery = new LambdaQueryWrapper<Reservations>()
                    .eq(Reservations::getUserid, userid)
                    .eq(Reservations::getBookid, book.getBookid());
            remove(resQuery);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
